/**
 * 表单数据封装
 */
(function ($) {
    $.extend({
        vali: {
            /**
             *
             * @param idcode 身份证号
             * @returns {boolean} true 正确 false 错误
             */
            valiCard: function (idcode) {
                // 加权因子
                var weight_factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
                // 校验码
                var check_code = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
                var code = idcode + "";
                var last = idcode[17];//最后一位
                var seventeen = code.substring(0, 17);
                // ISO 7064:1983.MOD 11-2
                // 判断最后一位校验码是否正确
                var arr = seventeen.split("");
                var len = arr.length;
                var num = 0;
                for (var i = 0; i < len; i++) {
                    num = num + arr[i] * weight_factor[i];
                }
                // 获取余数
                var resisue = num % 11;
                var last_no = check_code[resisue];
                var idcard_patter = /^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/;
                // 判断格式是否正确
                var format = idcard_patter.test(idcode);

                // 返回验证结果，校验码和格式同时正确才算是合法的身份证号码
                return last === last_no & format ? true : false;
            },
            valiCardIsOk: function (obj) {
                // 加权因子
                var weight_factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
                // 校验码
                var check_code = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
                var code = obj.value + "";
                var last = obj.value[17];//最后一位
                var seventeen = code.substring(0, 17);
                // ISO 7064:1983.MOD 11-2
                // 判断最后一位校验码是否正确
                var arr = seventeen.split("");
                var len = arr.length;
                var num = 0;
                for (var i = 0; i < len; i++) {
                    num = num + arr[i] * weight_factor[i];
                }
                // 获取余数
                var resisue = num % 11;
                var last_no = check_code[resisue];
                var idcard_patter = /^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$/;
                // 判断格式是否正确
                var format = idcard_patter.test(obj.value);
                if (!format){
                    obj.value = '';
                }
            },

            /**
             *
             * @param email 邮箱
             * @returns {boolean} true 正确 false 错误
             */
            valiEmail: function (email) {
                var reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
                return reg.test(email);
            },

            /**
             *
             * @param phone 手机号
             * @returns {boolean} true 正确 false 错误
             */
            valiPhone: function (phone) {
                var phoneTest = /^[1][3,4,5,7,8,9][0-9]{9}$/;
               return phoneTest.test(phone);
            },
            /**
             *
             * @param bankCard 银行卡号
             * @returns {boolean} true 正确 false 错误
             */
            bankCard: function (bankCard) {
                var regExp = /^([1-9]{1})(\d{15}|\d{18})$/;
                return regExp.test(bankCard)
            },
            /**
             *
             * @param id 统一社会信用代码:
             * @returns {boolean}
             */
            id: function (id) {
                var regExp = /^[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}$/g;
                return regExp.test(id)
            },
            /**
             *
             * @param money 金额:
             * @returns {boolean}
             */
            money: function (money) {
                var regExp = /((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/;
                return regExp.test(money)
            },
            /**
             * 输入金额时的验证
             * @param obj
             */
            changeMoney: function (obj) {
                obj.value = obj.value.replace(/[^\d.]/g, '').replace(/\.{2,}/g, '.').replace('.', '$#$').replace(/\./g, '').replace('$#$', '.').replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3').replace(/^\./g, '');
                //对于010.22,001.12格式金额不给通过
                var index = obj.value.indexOf(".");
                var str = obj.value.substring(obj.value,0,index);
                var firstNums = obj.value.substring(obj.value,0,1);
                if (index > -1 && str.length >= 2 && firstNums <= 0){
                    obj.value = '';
                }
            },
        }
    });
})(jQuery);